package com.mayo.series;

import java.security.acl.Group;
import java.util.TreeSet;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.ads.AdView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.mayo.series.alarmas.AlarmManagerBroadcastReceiver;
import com.mayo.series.comun.BaseMainActivity;
import com.mayo.series.comun.ComunUtilidades;

public class MainActivity extends BaseMainActivity {

	private SlidingMenu slidingMenu;
	private AlarmManagerBroadcastReceiver alarmaManager = new AlarmManagerBroadcastReceiver();
	
	SparseArray<Group> groups = new SparseArray<Group>();
	
	private AdView adView;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        adView = ComunUtilidades.startAdview(this);
        
        setBehindContentView(R.layout.slidingmenu);
        getSlidingMenu().setBehindOffset(R.dimen.slidingmenu_offset);
        
        getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.ic_action_overflow));
        
        slidingMenu = new SlidingMenu(this);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//        slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
//        slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        slidingMenu.setFadeDegree(0.35f);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(R.layout.slidingmenu);
        
        final ListView listview = (ListView) slidingMenu.findViewById(R.id.listview);
        
        String[] values = new String[] { 
        		getString(R.string.menu_entrada_mis_series),
        		getString(R.string.menu_entrada_mis_favoritas),
        		getString(R.string.menu_separador_emision), // Separador 
        		getString(R.string.menu_entrada_listado),
        		getString(R.string.menu_entrada_calendario),
        		getString(R.string.menu_separador_recomendadas), // Separador
        		getString(R.string.menu_entrada_recomendadas),
        		//getString(R.string.menu_entrada_nuevas), 
        		getString(R.string.menu_entrada_ajustes)};
        String[] descriptions = new String[] { 
        		getString(R.string.menu_subentrada_series),
        		getString(R.string.menu_subentrada_favoritas),
        		"", // Separador
        		getString(R.string.menu_subentrada_ordenado),
        		getString(R.string.menu_subentrada_calendario),
        		"", // Separador
        		getString(R.string.menu_subentrada_recomendadas), 
        		//getString(R.string.menu_subentrada_nuevas), 
        		getString(R.string.menu_subentrada_ajustes)};
        int[] icons = { R.drawable.ic_action_favorite_event,
        		R.drawable.ic_action_favorite_on,
        		0, // Separador
        		R.drawable.ic_action_view_as_list, 
        		R.drawable.ic_action_go_to_today,
        		0, // Separador
        		R.drawable.ic_action_good,
        		//R.drawable.ic_action_event,
        		R.drawable.ic_action_overflow};
        Class[] clases = { MisSeriesActivity.class, 
        		MisSeriesFavoritasActivity.class,
        		null, // Separador
        		ListadoSeriesActivity.class, 
        		CalendarioSeriesActivity.class,
        		null, // Separador
        		RecomendacionSeriesActivity.class, 
        		//NuevasSeriesActivity.class, 
        		AjustesAppActivity.class};

        MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(getApplicationContext(), values, descriptions, icons, clases);
        listview.setAdapter(adapter);
        
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE
				| ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP
				| ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
        // Cargo la lista de noticias
        listaNoticias = (ListView) findViewById(R.id.lista_noticias);
        new LoadAllNoticias().execute();
    }
	
	@Override
	  public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main, menu);
	    return true;
	  } 
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.slidingMenu.toggle();
            return true;
        case R.id.action_settings:
        	Intent intent = new Intent(getApplicationContext(), AjustesAppActivity.class);
        	startActivity(intent);
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    	  private final Context context;
    	  private final String[] values;
    	  private final String[] descriptions;
    	  private final Class[] clases;
    	  private final int[] icons;
    	  private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

    	  public MySimpleArrayAdapter(Context context, String[] values, String[] descriptions, int[] icons, Class[] clases) {
    	    super(context, R.layout.slidingmenu_layout, values);
    	    this.context = context;
    	    this.values = values;
    	    this.descriptions = descriptions;
    	    this.icons = icons;
    	    this.clases = clases;
    	  }

    	  @Override
    	  public View getView(final int position, View convertView, ViewGroup parent) {
    	    LayoutInflater inflater = (LayoutInflater) context
    	        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	    View rowView = null;
    	    if (clases[position] == null)
    	    {
    	    	rowView = inflater.inflate(R.layout.list_header, null);
                TextView separador = (TextView)rowView.findViewById(R.id.texto_separador);
                separador.setText(values[position]);
    	    } else {
	    	    rowView = inflater.inflate(R.layout.slidingmenu_layout, parent, false);
	    	    TextView label = (TextView) rowView.findViewById(R.id.label);
	    	    TextView desc = (TextView) rowView.findViewById(R.id.desc);
	    	    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
	    	    label.setText(values[position]);
	    	    desc.setText(descriptions[position]);
	   	    	imageView.setImageDrawable(getResources().getDrawable(icons[position]));
	
	   	    	rowView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(getApplicationContext(), clases[position]);
						startActivity(intent);
					}
				});
    	    }
    	    return rowView;
    	  }
    }
	
    @Override
    protected void onStart() {
    	super.onStart();
    	
        ComunUtilidades.configurarAlarmas(getApplicationContext(), alarmaManager);
    }
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    }
}
